package net.dodoman.notepad;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

//查找实现
class FindDialog extends JDialog implements ActionListener, WindowListener,
		KeyListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5139823602546694898L;
	final JTextArea text;
	JLabel lfind, lreplace;
	JTextField tfind, treplace;
	JButton bfind, breplace, bnext, bcancel;
	JPanel fieldPane, buttonPane;
	String strFind, strReplace;
	int txtPlace, txtLen, strNext;

	FindDialog(final JTextArea text, JFrame findDialog) {
		super(findDialog, "查找/替换", false);
		this.text = text;
		Container findPane = getContentPane();
		lfind = new JLabel("查找内容");
		lreplace = new JLabel("替换");
		tfind = new JTextField();
		treplace = new JTextField();
		bfind = new JButton("查找", new ImageIcon("ico/Find16.gif"));
		breplace = new JButton("替换", new ImageIcon("ico/Replace16.gif"));
		bcancel = new JButton("取消", new ImageIcon("ico/exit.gif"));
		bnext = new JButton("下一个", new ImageIcon("ico/FindAgain16.gif"));
		fieldPane = new JPanel();
		buttonPane = new JPanel();
		fieldPane.setLayout(new GridLayout(2, 2));
		findPane.add(fieldPane, BorderLayout.CENTER);
		findPane.add(buttonPane, BorderLayout.SOUTH);
		fieldPane.add(lfind);
		fieldPane.add(tfind);
		fieldPane.add(lreplace);
		fieldPane.add(treplace);
		buttonPane.add(bfind);
		buttonPane.add(breplace);
		buttonPane.add(bnext);
		buttonPane.add(bcancel);
		bfind.addActionListener(this);
		breplace.addActionListener(this);
		bcancel.addActionListener(this);
		this.addWindowListener(this);
		tfind.addKeyListener(this);
		treplace.addKeyListener(this);
		bnext.addActionListener(this);

		setSize(370, 120);
		setResizable(false);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == bfind) {
			find();
		}
		if (e.getSource() == breplace) {
			replace();
		}
		if (e.getSource() == bcancel) {
			setVisible(false);
		}
		if (e.getSource() == bnext) {
			findNext();
		}
	}

	// 查找处理
	private void find() {
		strFind = tfind.getText();
		strNext = 0; // 开始位置
		txtPlace = text.getText().indexOf(strFind, strNext);
		txtLen = txtPlace + strFind.length();
		text.select(txtPlace, txtLen);
		strNext = text.getSelectionEnd(); // 选中内容的最后位置
		if (txtPlace == -1) /* 没有找到的处理 */{
			JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找",
					JOptionPane.INFORMATION_MESSAGE);
		}
		bnext.setEnabled(true);
	}

	// 替换处理
	private void replace() {
		strReplace = treplace.getText();
		text.replaceRange(strReplace, txtPlace, txtPlace + strFind.length());
	}

	// 查找下一个处理
	private void findNext() {
		bfind.setEnabled(false);
		txtPlace = text.getText().indexOf(strFind, strNext);
		txtLen = txtPlace + strFind.length();
		text.select(txtPlace, txtLen);
		strNext = text.getSelectionEnd(); // 获取查找下一个内容的最后位置
		if (txtPlace == -1) /* 没有找到的处理 */{
			JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找下一个",
					JOptionPane.INFORMATION_MESSAGE);
			strNext = 0; // 没有找到初始化位置,以变重新查找
			tfind.setText("");
			bnext.setEnabled(false);
			breplace.setEnabled(false);
		}
	}

	public void windowOpened(WindowEvent e) {
		bfind.setEnabled(false);
		breplace.setEnabled(false);
		bnext.setEnabled(false);
	}

	public void windowClosing(WindowEvent e) {
		// TODO 自动生成方法存根
	}

	public void windowClosed(WindowEvent e) {
		// TODO 自动生成方法存根
	}

	public void windowIconified(WindowEvent e) {
		// TODO 自动生成方法存根
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO 自动生成方法存根
	}

	public void windowActivated(WindowEvent e) {
		// TODO 自动生成方法存根
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO 自动生成方法存根
	}

	public void keyTyped(KeyEvent e) {
		// TODO 自动生成方法存根
	}

	public void keyPressed(KeyEvent ke) {
		if (ke.getSource() == tfind)
			bfind.setEnabled(true);
		if (ke.getSource() == treplace)
			breplace.setEnabled(true);
	}

	public void keyReleased(KeyEvent e) {
		// TODO 自动生成方法存根
	}
}
